//
//  YDMineControllerTopView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/9.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDMineControllerTopView.h"

@interface YDMineControllerTopView()
//背景图片
@property (nonatomic , weak) UIImageView *backImage;
//标题
@property (nonatomic , weak) UILabel *titleLabel;
//用户头像
@property (nonatomic , weak) UIImageView *iconImageView;
//账户余额
@property (nonatomic , weak) UILabel *accountLabel;
//账户余额数值
@property (nonatomic , weak) UILabel *accountNumber;
//充值按钮
@property (nonatomic , weak) SYNoHeightedButton *chargeButton;
//提现按钮
@property (nonatomic , weak) SYNoHeightedButton *withdrawalsButton;

@end

@implementation YDMineControllerTopView

-(void)setDict:(NSDictionary *)dict
{
    _dict = dict;
    
    //账户可用余额
    NSString *str1 = @"￥";
    NSString *accountString = [[NSString stringWithFormat:@"%@" , dict[@"availableBalance"]] pz_stringNumberByMultiplyingBy:@"1"];
    
    NSString *formateAccount = [NSString countNumAndChangeformat:accountString];
    if ([formateAccount isEqualToString:@"0.00"]) {
        formateAccount = @"0";
    }
    
    NSString *str2 = [NSString stringWithFormat:@"%@%@",str1 , formateAccount];
    self.accountNumber.attributedText = [NSString getAttriNeedChangeString:str1 changeColor:[UIColor whiteColor] changeFont:[UIFont systemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:str2];
  
    NSString *iconUrl = dict[@"headPortrait"];
    DDLog(@"头像地址 = %@" , dict[@"headPortrait"])
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageNamed:@"wd-touxiang"]];
    
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.userInteractionEnabled = YES;
        [self backImage];
        [self titleLabel];//标题
        [self iconImageView];//用户头像
        [self accountLabel];//账户余额
        [self accountNumber];//账户余额数值
        [self chargeButton];
        [self withdrawalsButton];
        [self notiView];
    }
    return self;
}

+(instancetype)mineControllerTopViewInitWithFrame:(CGRect)frame andDict:(NSDictionary *)dict
{
    YDMineControllerTopView *topView = [[self alloc] initWithFrame:frame];
    topView.dict = dict;
    return topView;
}
/*****背景图片******/
-(UIImageView *)backImage
{
    if (_backImage == nil) {
        UIImageView *backImage = [[UIImageView alloc] init];
        [self addSubview:backImage];
        backImage.image = [UIImage imageNamed:@"wode-bg-yd"];
        backImage.contentMode = UIViewContentModeScaleAspectFill;
        [backImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.equalTo(self);
        }];
        _backImage = backImage;
    }
    return _backImage;
}
/*****标题******/
-(UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        UILabel *titleLabel = [[UILabel alloc] init];
        [self addSubview:titleLabel];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.font = [UIFont systemFontOfSize:font20ScreenWRate * LKScreenW];
        titleLabel.text = @"我的";
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            if (isPhoneX) {
                make.top.equalTo(self).offset(rateX812(37));
            }else{
                make.top.equalTo(self).offset(rate750(27));
            }
            
        }];
        
        _titleLabel = titleLabel;
    }
    return _titleLabel;
}

/*****用户头像******/
-(UIImageView *)iconImageView
{
    if (_iconImageView == nil) {
        UIImageView *iconImageView = [[UIImageView alloc] init];
        [self addSubview:iconImageView];
        iconImageView.image = [UIImage imageNamed:@"wd-touxiang"];
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.height.width.equalTo(@(rate375(60)));
            if (isPhoneX) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(rateX812(22));
            }else{
                make.top.equalTo(self.titleLabel.mas_bottom).offset(rate750(22));
            }
        }];
        iconImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [iconImageView addGestureRecognizer:tap];
        iconImageView.layer.cornerRadius = rate375(60) * 0.5;
        iconImageView.layer.masksToBounds = YES;
        
        _iconImageView = iconImageView;
    }
    return _iconImageView;
}

//*********账户余额*************/
-(UILabel *)accountLabel
{
    if (_accountLabel == nil) {
        UILabel *accountLabel = [[UILabel alloc] init];
        [self addSubview:accountLabel];
        accountLabel.textAlignment = NSTextAlignmentCenter;
        accountLabel.textColor = DRJAlphaColor(255, 255, 255, 0.7);
        accountLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        accountLabel.text = @"账户余额";
        [accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            if (isPhoneX) {
                make.top.equalTo(self.iconImageView.mas_bottom).offset(rateX812(16));
            }else{
                make.top.equalTo(self.iconImageView.mas_bottom).offset(rate750(16));
            }
            
        }];
        _accountLabel = accountLabel;
    }
    return _accountLabel;
}
//*********账户余额数值*************/
-(UILabel *)accountNumber
{
    if (_accountNumber == nil) {
        UILabel *accountNumber = [[UILabel alloc] init];
        [self addSubview:accountNumber];
        accountNumber.textAlignment = NSTextAlignmentCenter;
        accountNumber.textColor = DRJAlphaColor(255, 255, 255, 1.0);
        accountNumber.font = [UIFont boldSystemFontOfSize:font28ScreenWRate * LKScreenW];
//        accountNumber.text = @"￥8888.88";
        [accountNumber mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            if (isPhoneX) {
                make.top.equalTo(self.accountLabel.mas_bottom).offset(rateX812(2));
            }else{
                make.top.equalTo(self.accountLabel.mas_bottom).offset(rate750(2));
            }
            
        }];
        NSString *str1 = @"￥";
        NSString *str2 = @"￥8888.88";
        accountNumber.attributedText = [NSString getAttriNeedChangeString:str1 changeColor:[UIColor whiteColor] changeFont:[UIFont systemFontOfSize:font12ScreenWRate * LKScreenW] sourceString:str2];
        
        
        
        _accountNumber = accountNumber;
    }
    return _accountNumber;
}
//*********充值按钮*************/
-(SYNoHeightedButton *)chargeButton
{
    if (_chargeButton == nil) {
        SYNoHeightedButton *chargeButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:chargeButton];
        chargeButton.backgroundColor = [UIColor colorWithHexString:Orange];
        [chargeButton setTitle:@"充值" forState:UIControlStateNormal];
        [chargeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        chargeButton.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        chargeButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [chargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_centerX).offset(-rate375(10));
            make.width.equalTo(@(rate375(120)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(32)));
                make.top.equalTo(self.iconImageView.mas_bottom).offset(rateX812(81));
            }else{
                make.height.equalTo(@(rate750(32)));
                make.top.equalTo(self.iconImageView.mas_bottom).offset(rate750(81));
            }
        }];
        chargeButton.layer.cornerRadius = 3.0;
        chargeButton.layer.masksToBounds = YES;
        [chargeButton addTarget:self action:@selector(chargeButtonClick) forControlEvents:UIControlEventTouchDown];
        _chargeButton = chargeButton;
    }
    return _chargeButton;
}

//*********提现按钮*************/
-(SYNoHeightedButton *)withdrawalsButton
{
    if (_withdrawalsButton == nil) {
        SYNoHeightedButton *withdrawalsButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:withdrawalsButton];
        withdrawalsButton.backgroundColor = [UIColor clearColor];
        [withdrawalsButton setTitle:@"提现" forState:UIControlStateNormal];
        [withdrawalsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        withdrawalsButton.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        withdrawalsButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [withdrawalsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_centerX).offset(rate375(10));
            make.width.equalTo(@(rate375(120)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(32)));
                make.top.equalTo(self.iconImageView.mas_bottom).offset(rateX812(81));
            }else{
                make.height.equalTo(@(rate750(32)));
                make.top.equalTo(self.iconImageView.mas_bottom).offset(rate750(81));
            }
        }];
        withdrawalsButton.layer.cornerRadius = 3.0;
        withdrawalsButton.layer.masksToBounds = YES;
        withdrawalsButton.layer.borderColor = [UIColor whiteColor].CGColor;
        withdrawalsButton.layer.borderWidth = 1.0f;
        [withdrawalsButton addTarget:self action:@selector(withdrawalsButtonClick) forControlEvents:UIControlEventTouchDown];
        _withdrawalsButton = withdrawalsButton;
    }
    return _withdrawalsButton;
}

/**
 消息提醒view
 */
- (YDMineNotiView *)notiView
{
    if (_notiView == nil) {
        _notiView = [[YDMineNotiView alloc] init];
        [self addSubview:_notiView];
        [_notiView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(rate375(16)));
            
            make.centerY.equalTo(self->_titleLabel.mas_centerY);
            
            make.width.equalTo(@25);
            make.height.equalTo(@20);
        }];
        LK(weself);
        [_notiView setClickBack:^{
            if (weself.delegate && [weself.delegate respondsToSelector:@selector(mineControllerTopView:clickStyle:)]) {
                 [weself.delegate mineControllerTopView:weself clickStyle:SYMineTopViewClickWithNoti];
            }
        }];
    }
    return _notiView;
}

-(void)chargeButtonClick
{
    if ([self.delegate respondsToSelector:@selector(mineControllerTopView:clickStyle:)]) {
        [self.delegate mineControllerTopView:self clickStyle:SYMineTopViewClickCharge];
    }
}
-(void)withdrawalsButtonClick
{
    if ([self.delegate respondsToSelector:@selector(mineControllerTopView:clickStyle:)]) {
        [self.delegate mineControllerTopView:self clickStyle:SYMineTopViewClickWithButton];
    }
}
-(void)tapClick
{
    if ([self.delegate respondsToSelector:@selector(mineControllerTopView:clickStyle:)]) {
        [self.delegate mineControllerTopView:self clickStyle:SYMineTopViewClickAcc];
    }
}
@end
